<?php
namespace app\api\controller;

use think\Controller;
use Firebase\JWT\JWT;

/**
 * 公用基础控制器
 */
class Base extends Controller {

	protected $uid;

	protected function _initialize() {
		parent::_initialize();
		$this->request->filter('htmlspecialchars,trim_all');
		header('Access-Control-Allow-Origin: *');
		header('Access-Control-Allow-Headers:token');
		//验证key,是否非法访问
	}

	protected function jwt_token($id, $mobile, $exp = '172800') {
		$nowtime = time();
		$token = array(
			'iss' => 'https://s.qiansuipay.com', //签发者
			'aud' => 'https://s.qiansuipay.com', //jwt所面向的用户
			'iat' => $nowtime, //签发时间
			'exp' => $nowtime + $exp, //过期时间-10min
			'data' => array(
				'id' => $id,
				'mobile' => $mobile 
			)
		);
		$jwt = JWT::encode($token, config('JWT_KEY'));
		return $jwt;
	}

	protected function check_token() {
		$json['msg'] = '';
		$json['error'] = false;
		$json['code'] = -1;
		//验证key,是否非法访问
		$jwt = isset($_SERVER['HTTP_TOKEN']) ? $_SERVER['HTTP_TOKEN'] : '';
		if (empty($jwt)) {
			$json['msg'] = 'Access No';
			$json['error'] = true;
		} else {
			try {
				JWT::$leeway = 60;
				$decoded = JWT::decode($jwt, config('JWT_KEY'), array('HS256'));
				$arr = (array)$decoded;
				if ($arr['exp'] < time()) {
					$json['msg'] = 'Token Expired';
					$json['error'] = true;
				}
				$this->uid = $arr['data']->id;
				$member = db('member')->find($arr['data']->id);
				$member = db('member')->where('mobile', $mobile)->where('password', $password)->find();
				if (!$member) {
					$json['msg'] = '会员身份验证错误，强制下线';
					$json['error'] = true;
				} else {
					// 判断是否多点登录
					if (strcmp($member['token'], $jwt) != 0) {
						$json['msg'] = '账号在别处登录您被强制下线了';
						$json['error'] = true;
					}
				}
			} catch(\Exception $e) {
				$json['msg'] = 'Token Failed';
				$json['error'] = true;
			}
		}
		if ($json['error']) {
			json($json)->send();
			exit ;
		}
	}

	/**
	 * 返回成功带提示信息
	 * @param msg 操作成功或操作失败
	 * @param error=false
	 */
	protected function toSuccess($msg = '操作成功', $data = '') {
		$json['msg'] = $msg;
		$json['data'] = $data;
		$json['code'] = 1;
		return json($json);
	}

	/**
	 * 返回失败带提示信息
	 * @param msg
	 * @param data 可以为空
	 * @param error=true
	 */
	protected function toError($msg = '没有查到数据') {
		$json['msg'] = $msg;
		$json['code'] = 0;
		return json($json);
	}
	//优惠券变化日志表
 	protected function add_scorelog($type,$mess,$balance,$member_id,$zz_id=0,$sk_id=0,$fh_id=0){
 		$data['type']=$type;
		$data['message']=$mess;
		$data['balance']=$balance;
		$data['member_id']=$member_id;
		$data['zz_id']=$zz_id;
		$data['sk_id']=$sk_id;
		$data['add_time']=now_time();
		$data['fh_id']=$fh_id;
		return db('m_score_charge')->insert($data);
 	}
	/**
	 * 返回成功不带提示信息
	 * @param data
	 * @param error=false
	 */
	protected function toData($data) {
		$json['data'] = $data;
		$json['code'] = 1;
		return json($json);
	}

 

	//*判断图片上传格式是否为图片 return返回文件后缀
	protected function getImagetype($filename) {
		$file = fopen($filename, 'rb');
		$bin = fread($file, 2);
		//只读2字节
		fclose($file);
		$strInfo = @unpack('C2chars', $bin);
		$typeCode = intval($strInfo['chars1'] . $strInfo['chars2']);
		// dd($typeCode);
		$fileType = '';
		switch ($typeCode) {
			case 255216 :
				$fileType = 'jpg';
				break;
			case 7173 :
				$fileType = 'gif';
				break;
			case 6677 :
				$fileType = 'bmp';
				break;
			case 13780 :
				$fileType = 'png';
				break;
			default :
				$fileType = 'error';
		}
		return $fileType;
	}


}
